package com.liang.maogouyuyin.network

import com.liang.maogouyuyin.bean.ArticleBean
import com.liang.maogouyuyin.bean.VarietyBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup

object RemoteRepository {

    suspend fun getCatVariety(): ArrayList<VarietyBean> {
        val url = "https://www.chongwy.com/maodq/"
        return getVariety(url)
    }

    suspend fun getDogVariety(): ArrayList<VarietyBean> {
        val url = "https://www.chongwy.com/goudq/"
        return getVariety(url)
    }

    private suspend fun getVariety(url: String): ArrayList<VarietyBean> {
        return withContext(Dispatchers.IO) {
            val doc = Jsoup.connect(url).get()
            val contentElement = doc.select(".list")
            val contentElements = contentElement.select("li")

            val list = ArrayList<VarietyBean>()

            for (element in contentElements) {
                val href = element.select("a[href]").first()?.attr("href")
                val image = element.select("img").first()?.attr("jxlazyimg")
                val title = element.select("a.title").first()?.text()
                list.add(VarietyBean(href!!, image!!, title!!))
            }

            list
        }
    }

    suspend fun getVarietyDetail(url: String): String {
        val baseUrl = "https://www.chongwy.com$url"
        return withContext(Dispatchers.IO) {
            val doc = Jsoup.connect(baseUrl).get()
            val contentElement = doc.select(".left")[1]
            val cleanHtml = """
            <!DOCTYPE html>
            <html>
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <style>
                        body { font-family: sans-serif; line-height: 1.6; padding: 16px; margin: 0 auto; max-width: 800px; }
                        img { max-width: 100%; height: auto; }
                    </style>
                </head>
                <body>
                    ${contentElement.html()}
                </body>
            </html>
        """.trimIndent()
            cleanHtml
        }
    }

    suspend fun getTraining(): ArrayList<ArticleBean> {
        return withContext(Dispatchers.IO) {

            val url = "https://www.pmshe.com/encyclopedia/behavior-and-training"
            val doc = Jsoup.connect(url).get()
            val contentElement = doc.select(".sec-panel-body")
            val contentElements = contentElement.select("li")

            val list = ArrayList<ArticleBean>()

            for (element in contentElements) {
                val href = element.select(".item-title").first()?.select("a[href]")?.first()?.attr("href")
                val title = element.select(".item-title").first()?.select("a[href]")?.first()?.text()
                val content = element.select(".item-excerpt").first()?.text()
                list.add(ArticleBean(title!!, content!!, href!!))
            }

            list
        }
    }

    suspend fun getTrainingArticle(url: String): String {
        return withContext(Dispatchers.IO) {
            val doc = Jsoup.connect(url).get()
            val contentElement = doc.select(".entry-content")
            val cleanHtml = """
            <!DOCTYPE html>
            <html>
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <style>
                        body { font-family: sans-serif; line-height: 1.6; padding: 16px; margin: 0 auto; max-width: 800px; }
                        img { max-width: 100%; height: auto; }
                    </style>
                </head>
                <body>
                    ${contentElement.html().split("<h3 class=\"wp-block-heading\"")[0]}
                </body>
            </html>
        """.trimIndent()
            cleanHtml
        }
    }

    suspend fun getAnimal(): ArrayList<ArticleBean> {
        return withContext(Dispatchers.IO) {

            val url = "https://www.pmshe.com/encyclopedia/care/basics"
            val doc = Jsoup.connect(url).get()
            val contentElement = doc.select(".sec-panel-body")
            val contentElements = contentElement.select("li")

            val list = ArrayList<ArticleBean>()

            for (element in contentElements) {
                val href = element.select(".item-title").first()?.select("a[href]")?.first()?.attr("href")
                val title = element.select(".item-title").first()?.select("a[href]")?.first()?.text()
                val content = element.select(".item-excerpt").first()?.text()
                list.add(ArticleBean(title!!, content!!, href!!))
            }

            list
        }
    }

    suspend fun getAnimalArticle(url: String): String {
        return withContext(Dispatchers.IO) {
            val doc = Jsoup.connect(url).get()
            val contentElement = doc.select(".entry-content")
            val cleanHtml = """
            <!DOCTYPE html>
            <html>
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <style>
                        body { font-family: sans-serif; line-height: 1.6; padding: 16px; margin: 0 auto; max-width: 800px; }
                        img { max-width: 100%; height: auto; }
                    </style>
                </head>
                <body>
                    ${contentElement.html().split("<h3 class=\"wp-block-heading\"")[0]}
                </body>
            </html>
        """.trimIndent()
            cleanHtml
        }
    }
}